package com.iwomy.secureplat.platform.service.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iwomy.secureplat.common.data.datascope.DataScope;
import com.iwomy.secureplat.common.data.tenant.TenantContextHolder;
import com.iwomy.secureplat.platform.dto.BasicNotificationRecordDTO;
import com.iwomy.secureplat.platform.entity.*;
import com.iwomy.secureplat.platform.mapper.BasicNotificationRecordMapper;
import com.iwomy.secureplat.platform.service.BasicDeviceService;
import com.iwomy.secureplat.platform.service.BasicNotificationRecordService;
import com.iwomy.secureplat.platform.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 通知记录
 *
 * @author pig
 * @date 2023-12-26 11:49:32
 */
@Service
public class BasicNotificationRecordServiceImpl extends ServiceImpl<BasicNotificationRecordMapper, BasicNotificationRecordEntity> implements BasicNotificationRecordService {

	@Autowired
	private BasicDeviceService basicDeviceService;

	@Override
	public BasicNotificationRecordEntity oneBySupplierIdentifying(String supplierIdentifying) {
		TenantContextHolder.setTenantSkip();
		LambdaQueryWrapper<BasicNotificationRecordEntity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(BasicNotificationRecordEntity::getSupplierIdentifying, supplierIdentifying);
		return this.getOne(queryWrapper);
	}

	//通过手机号查询接警人通知记录
	@Override
	public Page getBasicNotificationRecordPage(Page page, BasicNotificationRecordDTO basicNotificationRecord) {
		LambdaQueryWrapper<BasicNotificationRecordEntity> wrapper = Wrappers.lambdaQuery();
		List<BasicNotificationRecordEntity> list = this.baseMapper.selectListByScope(wrapper, new DataScope());
		List<Long> notificationId = list.stream().map(BasicNotificationRecordEntity::getId).collect(Collectors.toList());

		if (ObjectUtil.isEmpty(notificationId)) return page;
		LambdaQueryWrapper<BasicNotificationRecordEntity> queryWrapper = Wrappers.lambdaQuery();
		if (ArrayUtil.isNotEmpty(basicNotificationRecord.getCreateTime())) {
			queryWrapper.ge(BasicNotificationRecordEntity::getCreateTime, basicNotificationRecord.getCreateTime()[0]).le(BasicNotificationRecordEntity::getCreateTime,
					basicNotificationRecord.getCreateTime()[1]);
		}
		queryWrapper.eq(ObjectUtil.isNotEmpty(basicNotificationRecord.getNotificationType()),
				BasicNotificationRecordEntity::getNotificationType, basicNotificationRecord.getNotificationType());

		queryWrapper.eq(ObjectUtil.isNotEmpty(basicNotificationRecord.getPhone()),
				BasicNotificationRecordEntity::getReceiverId, basicNotificationRecord.getPhone());

		queryWrapper.in(ObjectUtil.isNotEmpty(notificationId), BasicNotificationRecordEntity::getId, notificationId);

		Page<BasicNotificationRecordEntity> notificationRecord = baseMapper.selectPage(page, queryWrapper);

		// 获取当前页的设备ID集合
		List<Long> deviceIds = notificationRecord.getRecords().stream()
				.map(BasicNotificationRecordEntity::getDeviceId)
				.collect(Collectors.toList());

		if (ObjectUtil.isEmpty(deviceIds)) {
			return page;
		}
		// 批量查询设备信息
		List<BasicDeviceEntity> devices = basicDeviceService.listByIds(deviceIds);

		List<BasicNotificationRecordVO> notificationRecordVOS = new ArrayList<>();
		for (BasicNotificationRecordEntity record : notificationRecord.getRecords()) {
			BasicNotificationRecordVO vo = new BasicNotificationRecordVO();
			BeanUtils.copyProperties(record, vo);
			Long deviceId = record.getDeviceId();
			BasicDeviceEntity device = devices.stream()
					.filter(d -> d.getId().equals(deviceId)).findFirst().orElse(null);
			if (device != null) {
				vo.setImei(device.getImei());
			}
			notificationRecordVOS.add(vo);
		}

		Page<BasicNotificationRecordVO> voPage = new Page<>(notificationRecord.getCurrent(), notificationRecord.getSize());
		voPage.setTotal(notificationRecord.getTotal());
		voPage.setRecords(notificationRecordVOS);
		return voPage;
	}
}